package com.cordys.uc.webapps.translator;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InlineJavascriptTranslator {

    private StringBuilder srcContent;

    InlineJavascriptTranslator(StringBuilder srcContent) {
        this.srcContent = srcContent;
    }

    String translate() {
        return srcContent.toString();
    }

    static void processinlinejs(String str, String jsfile) {


        ArrayList<String> xldata = new ArrayList();

        System.out.println("Processing file....  " + jsfile);

        Pattern p = null;
        Matcher m = null;



        p = Pattern.compile("setLabel\\(|"
                + "application\\.notify\\(|"
                + "application\\.inform\\(|"
                + "application\\.showError\\(|"
                + "innerText|"
                + "innerHTML|"
                + "application\\.confirm\\(|"
                + "setTextContent\\([^\\;]*\\\"\\)\\;|"
                + "outerHTML|"
                + "value[\\s]*\\=[\\s]*\\\"|"
                + "setValue[\\s]*\\([\\s]*\\\"|"
                + "application\\.select|"
                + "showDialog|"
                + "\\.setCaption|"
                + "\\.setTitle|"
                + "\\.setTooltip"
                + "\\.setDialogTitle|"
                + "\\.title|"
                + "application\\.prompt");
        m = p.matcher(str);
        while (m.find()) {
            int linno = FileUtil.getLineNo(m.start(), str, str);

            String tmline = FileUtil.getLine(linno, str);
            if (tmline.contains("translate(")) {
                String tm = tmline.substring(tmline.indexOf("translate(") + "translate(".length() + 1);
                int tmin = tm.indexOf("\"");

                if (tmin != -1) {
                    String tmstr = tm.substring(0, tmin);
                    //add label to xldata List
                    xldata.add("<" + tmstr + "/>");
                }
            }

            if (!(FileUtil.getLine(linno, str).contains("translate("))) {
                //log exception
                TranslationLogger.log(m.group(), jsfile, linno, FileUtil.getLine(linno, str));
            }




        }

        //log all labels to excel file.
        TranslationLogger.writetoXL(jsfile, xldata);
    }
}
